Thumb

Differences between IEnumerable and IQueryable?

1/12/2020 6:48:20 AM

IEnumerable and IQueryable both are interfaces to dot net collections. IQueryable interface inherit from IEnumerable interface. What ever things IEnumerable can do IQueryable can also do but there is some things IQueryable can do IEnumerable can’t able to do. IEnumerable load full collection of data in memory then filter on the memory but IQueryable load the data in memory by filtering in on SQL server. This is the big difference but also many differences are IEnumerable and IQueryable. Now given bellow the example code and explain the code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Reflection;
using testForClass1;
namespace testFor
{
    public  class Program
    {
        static void Main(string[] args)
        {
            Student student = new Student();
            List<Student> stdList = new List<Student>() { new Student { ID=1,Name="Jesy"}, new Student { ID = 2, Name = "Jimi" } };
            //Load all data in memory
            IEnumerable<Student> myStd = stdList.ToList();
            IEnumerable<Student> myStdRe= myStd.Where(x => x.ID == 1);
            //Load only search data in memory
            IQueryable<Student> myStd2 = stdList.ToList<Student>();
            IEnumerable<Student> myStdRe2 = myStd2.Where(x => x.ID == 1).ToList<Student>();
            Console.Read();
        }
    }
    public class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }

    }
}

In this code we can see stdList. This contain the two-student object, when we load the data and find the specific object then IEnumerable load all data into memory then filter but other hand IQueryable only filter data load into the memory.